package cn.kgc.ubagnet.service.product;

import cn.kgc.ubagnet.mapper.ProImgMapper;
import cn.kgc.ubagnet.mapper.ProductMapper;
import cn.kgc.ubagnet.pojo.*;
import cn.kgc.ubagnet.service.protype.ProtypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    ProductMapper productMapper;

    @Resource
    ProtypeService protypeService;

    @Resource
    ProImgMapper proImgMapper;

    //二级查询菜单
    @Override
    public PageInfo<Product> selePro(Integer typeid) {
        if(typeid!=0){
            if(typeid!=28){
                PageHelper.startPage(0,8);
                PageHelper.orderBy("totalcount desc");
            }else{
                PageHelper.startPage(0,6);
                PageHelper.orderBy("totalcount desc");
            }
            ProductExample example=new ProductExample();
            ProductExample.Criteria criteria = example.createCriteria();
            criteria.andTypeidEqualTo(typeid);
            List<Integer> list=new ArrayList<>();
            list.add(typeid);
            List<Product> products = productMapper.seleByView_prodoct_protype(list);
            PageInfo<Product> pageInfo=new PageInfo<>(products);
            return pageInfo;
        }else{
            PageHelper.startPage(0,6);
            PageHelper.orderBy("totalcount desc");
            List<Product> products = productMapper.selectByExample(null);
            PageInfo<Product> pageInfo=new PageInfo<>(products);
            return pageInfo;
        }

    }

    @Override
    public PageInfo<Product> selePro(Integer typeid, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("totalcount desc");
        if(typeid!=null){
            ProductExample example=new ProductExample();
            ProductExample.Criteria criteria = example.createCriteria();
            criteria.andTypeidEqualTo(typeid);
            List<Integer> list=new ArrayList<>();
            list.add(typeid);
            List<Product> products = productMapper.seleByView_prodoct_protype(list);
            PageInfo<Product> pageInfo=new PageInfo<>(products);
            return pageInfo;
        }else{
            List<Product> products = productMapper.selectByExample(null);
            PageInfo<Product> pageInfo=new PageInfo<>(products);
            return pageInfo;
        }

    }

    //    按一级菜单查询
    @Override
    public PageInfo<Product> seleByView(Integer typeid) {
        //查询三级分类
        List<Integer> list=new ArrayList<>();
        List<Protype> protypes = protypeService.selePro(2,typeid);
        for (Protype protype : protypes) {
            List<Protype> level3 = protypeService.selePro(3, protype.getId());
            for (Protype i : level3) {
                list.add(i.getId());
            }
        }
//        //设置爆款显示数量
//        if(typeid==1||typeid==2||typeid==3||typeid==4||typeid==5){
//            PageHelper.startPage(0,6);
//        }else{
//            PageHelper.startPage(0,8);
//        }
        PageHelper.startPage(0,8);
        PageHelper.orderBy("totalcount desc");
        List<Product> products = productMapper.seleByView_prodoct_protype(list);
        PageInfo<Product> pageInfo=new PageInfo<>(products);
        return pageInfo;
    }




    @Override
    public PageInfo<Product> selebyDuotioajian(Integer typeid,Integer typeid2, Integer colorid, Integer sizeid, Double price1, Double price2, Integer pageNum, Integer pageSize, String desc,String proname) {

        //查询出来二级分类
        List<Integer> list=new ArrayList<>();
        if(typeid2!=-1){
            List<Protype> level3 = protypeService.selePro(3, typeid2);
            if(level3.size()>0&&level3!=null){
                for (Protype i : level3) {
                    list.add(i.getId());
                }
            }
        }else if(!"-1".equals(proname)){
            PageHelper.startPage(pageNum,pageSize);
            if("".equals(desc)){
                PageHelper.orderBy("totalcount desc");
            }else{
                PageHelper.orderBy(desc+" desc");
            }
            List<Product> products = productMapper.seleByView_prodoct_protypeconditions(list,colorid,sizeid,price1,price2,proname);
            PageInfo<Product> pageInfo=new PageInfo<>(products);
            return pageInfo;
        }else if(typeid!=null){
            List<Protype> protypes = protypeService.selePro(2,typeid);
            for (Protype protype : protypes) {
                List<Protype> level3 = protypeService.selePro(3, protype.getId());
                for (Protype i : level3) {
                    list.add(i.getId());
                }
            }
        }
        if(typeid2!=-1&& list.size()==0){
            list=null;
        }

        PageHelper.startPage(pageNum,pageSize);
        if("".equals(desc)){
            PageHelper.orderBy("totalcount desc");
        }else{
            PageHelper.orderBy(desc+" desc");
        }

        List<Product> products = productMapper.seleByView_prodoct_protypeconditions(list,colorid,sizeid,price1,price2,proname);
        PageInfo<Product> pageInfo=new PageInfo<>(products);
        return pageInfo;
    }


    //根据商品编号查询商品
    @Override
    public Product getProById(Integer id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public ProImg getProImgById(Integer id) {
        ProImgExample proImgExample=new ProImgExample();
        proImgExample.createCriteria().andIdEqualTo(id);
        List<ProImg> proImgs = proImgMapper.selectByExample(proImgExample);
        if(proImgs.size()>0){
            return proImgs.get(0);
        }
        return null;
    }

    @Override
    public Product seleByProName(String proname) {
    ProductExample example=new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        List<Product> products=new ArrayList<>();
        if(!"".equals(proname)){
            criteria.andPronameEqualTo(proname);
             products = productMapper.selectByExample(example);
        }
        return products.get(0);
    }

    @Override
    public int ProUpdate(Product product) {
        return productMapper.updateByPrimaryKeySelective(product);
    }
}
